/****************************************************************************
 * Copyright (C) 2011 Maxim Integrated Products, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated Products
 * shall not be used except as stated in the Maxim Integrated Products
 * Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products retains all ownership rights.
 *
 *     Description: MAXQ Wake-Up Timer Driver
 *             $Id: maxq_wut.c 2095 2011-09-22 19:04:58Z jbrodt $
 *
 ********************************************************************************
 */

#include "maxq_config.h"
#include "maxq_wut.h"

/***** Definitions *****/
#define WUT_MAX         0xFFFF  // 16-bit counter
#define WUT_CAL_CNTS    20      // number of nano-ring cycles to measure
#define TIMER_ENABLE    0x04    // T1CN register, bit TR1

/***** Function Prototypes *****/


/***** Global Data *****/


/***** File Scope Data *****/
static uint16_t WUT_rate = (WUT_FREQ / 100); // default value until calibrated (kHz * 10)


/****************************************************************************/
int wut_set_time(uint16_t ms)
{
  uint32_t WUT_value;

  // Calculates the number of counts 
  WUT_value = ((uint32_t)ms * (uint32_t)WUT_rate) / 10;

  // Check to see if count value is in the valid range for the wake-up timer
  if ( (WUT_value > WUT_MAX) || (WUT_value == 0) )
    return MAXQ_FAILURE; // returns 0, if cycles are out of range
  
  WUT = WUT_value;  // stores the number of cycles into the WUT register

  return MAXQ_SUCCESS;
}

/****************************************************************************/
int wut_calibrate(void)
{
  if (T1CN0 & TIMER_ENABLE)      // makes sure the timer is not already being used
    return MAXQ_FAILURE;

  // Setup the Wake-Up timer  
  wut_stop();          // make sure the wake-up timer is stopped
  WUT = WUT_CAL_CNTS;  // set the wake-up timer count value

  // Setup Timer 1-0 for measurement of the nano-ring frequency
  T1CN0 = 0x00;   // configure timer and clear the overflow flag
  T1MD0 = 0x01;   // count clock is /1, interrupt disabled
  T1L0 = 0x0000;  // clear the timer count value
  
  // Start the timers
  T1CN0 |= TIMER_ENABLE;
  wut_start();

  // Wait for wake-up timer to expire
  while (!(WUTC & WTF_MASK));

  // Start the timers
  T1CN0 &= ~TIMER_ENABLE;
  wut_stop();
  
  // Calculates the measured nano-ring frequency (kHz * 10)
  WUT_rate = (WUT_CAL_CNTS * (uint32_t)120000) / T1L0;

  return MAXQ_SUCCESS;
}
